﻿using JV.Com.Business;
using JV.Com.Internals;
using JV.Com.Model;
using JV.Com.ViewModels.Wrapper;
using Microsoft.Practices.Prism.StoreApps;
using Microsoft.Practices.Prism.StoreApps.Interfaces;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Input;
using Windows.ApplicationModel;
using Windows.UI.Xaml.Controls;

namespace JV.Com.ViewModels
{
    public sealed class MainPageViewModel : PageBaseViewModel
    {
        private readonly ObservableCollection<NewsWrapper> m_lastNews;
        private readonly ObservableCollection<GameArticleWrapper> m_lastTests;
        private readonly ObservableCollection<GameArticleWrapper> m_lastPreviews;
        private readonly ObservableCollection<GameArticleWrapper> m_lastFolders;

        public MainPageViewModel(INavigationService navigationService)
            : base(navigationService)
        {
            m_lastNews = new ObservableCollection<NewsWrapper>();
            m_lastTests = new ObservableCollection<GameArticleWrapper>();
            m_lastPreviews = new ObservableCollection<GameArticleWrapper>();
            m_lastFolders = new ObservableCollection<GameArticleWrapper>();
        }

        public override string PageTitle { get { return "Jeuxvideo.com"; } }

        public async override void OnNavigatedTo(object navigationParameter, Windows.UI.Xaml.Navigation.NavigationMode navigationMode, Dictionary<string, object> viewModelState)
        {
            base.OnNavigatedTo(navigationParameter, navigationMode, viewModelState);
            await InitHubAsync();
        }

        private async Task InitHubAsync()
        {
            var lastNews = await Service.GetLatestNewsAsync(0);
            foreach (var news in lastNews)
            {
                m_lastNews.Add(new NewsWrapper(this, news));
            }

            var lastTests = await Service.GetLatestArticlesAsync(ArticleType.Test, 0);
            foreach(var test in lastTests)
            {
                var existTest = m_lastTests.FirstOrDefault(t => t.Title == test.Title);
                if (existTest != null)
                {
                    existTest.Article.Machines.AddRange(test.Machines);
                }
                else
                {
                    m_lastTests.Add(GameArticleWrapper.Create(this, test));
                }
            }

            var lastPreviews = await Service.GetLatestArticlesAsync(ArticleType.Preview, 0);
            foreach(var preview in lastPreviews)
            {
                var existPreview = m_lastPreviews.FirstOrDefault(t => t.Title == preview.Title);
                if (existPreview != null)
                {
                    existPreview.Article.Machines.AddRange(preview.Machines);
                }
                else
                {
                    m_lastPreviews.Add(GameArticleWrapper.Create(this, preview));
                }
            }

            var lastFolders = await Service.GetLatestArticlesAsync(ArticleType.Folder, 0);
            foreach (var folder in lastFolders)
            {
                m_lastFolders.Add(GameArticleWrapper.Create(this, folder));
            }
        }

        public ObservableCollection<NewsWrapper> LastNews { get { return m_lastNews; } }
        public ObservableCollection<GameArticleWrapper> LastTests { get { return m_lastTests; } }
        public ObservableCollection<GameArticleWrapper> LastPreviews { get { return m_lastPreviews; } }
        public ObservableCollection<GameArticleWrapper> LastFolders { get { return m_lastFolders; } }

        public ICommand OpenNewsCommand
        {
            get
            {
                return new DelegateCommand<ItemClickEventArgs>((e) =>
                    {
                        NavigationService.Navigate("NewsList", ((NewsWrapper)e.ClickedItem).Id);
                    });
            }
        }

        public ICommand OnSectionHeaderClickCommand
        {
            get
            {
                return new DelegateCommand<HubSectionHeaderClickEventArgs>((e) =>
                    {
                        switch(e.Section.Name)
                        {
                            case "NewsSection":
                                NavigationService.Navigate("NewsList", m_lastNews[0].Id);
                                break;
                            default:
                                throw new NotSupportedException();
                        }
                    });
            }
        }

        public ICommand OnArticleClickCommand
        {
            get
            {
                return new DelegateCommand<ItemClickEventArgs>((e) =>
                    {
                        ((GameArticleWrapper)e.ClickedItem).OnClick();
                    });
            }
        }
    }
}
 